/*****************************************************************************
*                                                                           *
* Amazon S3 Command Shell  (s3shell)                                        *
*                                                                           *
* Copyright (C) 2008 Sumit Birla <http://sumitbirla.com>                    *
*                                                                           *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License for more details.                              *
*                                                                           *
* You should have received a copy of the GNU General Public License         *
* along with this program; if not, write to the Free Software               *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA  *
*                                                                           *
*****************************************************************************/

package com.sumitbirla.s3shell;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import com.amazon.s3.*;

/**
 *
 * @author sbirla
 */
public class Ls extends Command {
    
    public Ls(Environment env) {
        super(env);
    }
    
    @Override
    public void execute(String[] params, InputStream is, OutputStream os) {
        try {
            ArrayList masterList = new ArrayList();
            List objectList = env.s3conn.listBucket(env.bucket, null, null, null, null).entries;
            
            // each call returns at most 1000 object, so loop until all have been retrieved
            while (objectList.size() > 0)
            {
                masterList.addAll(objectList);
                ListEntry lastItem = (ListEntry)masterList.get(masterList.size()-1);
                objectList = env.s3conn.listBucket(env.bucket, null, lastItem.key, null, null).entries;
            }
                  
            // print out number of objects in bucket
            StringBuffer sb = new StringBuffer();
            sb.append("total " + masterList.size() + "\n");
            
            // print details of each object (one per line)
            for(Iterator it = masterList.iterator(); it.hasNext();)
            {  
                ListEntry item = (ListEntry)it.next();
                sb.append(item.owner.displayName + "\t");
                sb.append(item.size + "\t");
                sb.append(env.dateFormatter.format(item.lastModified) + "\t");
                sb.append(item.key + "\n");
            } 
            os.write(sb.toString().getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
